export enum SortType {
  ASC = 1,
  DESC = -1,
}

type compareVal = string | number;

/**
 * 排序
 * @param a
 * @param b
 * @param sortBy
 * @param sortType
 */
export function compareFn<T extends { [prop: string]: any }>(a: T, b: T, sortBy: string, sortType: SortType): number {
  let valueA: compareVal = a[sortBy];
  let valueB: compareVal = b[sortBy];
  let compare = 0;

  if(valueA === null || undefined){
    valueA = Number.MAX_SAFE_INTEGER
  }
  if(valueB === null || undefined){
    valueB = Number.MAX_SAFE_INTEGER
  }

  if (typeof valueA === 'string' && typeof valueB === 'string') {
    compare = valueA.localeCompare(valueB) * sortType
  }
  if (typeof valueA === 'number' && typeof valueB === 'number') {
    compare = valueA - valueB;
  }
  return compare * sortType;
}
